function collisionEngine() {
	this.collidables = [];
}

collisionEngine.prototype.addGameObject = function(go) {
	this.collidables.push(go);
}

collisionEngine.prototype.removeGameObject = function(go) {
    this.collidables.splice(this.findGameObjectIndex(go), 1);
}

collisionEngine.prototype.findGameObjectIndex = function (o) {
     /*var l = 0, u = this.collidables.length,  m;
     while ( l <= u ) { 
         if ( o.x > this.collidables[( m = Math.floor((l+u)/2) )].x ) l = m+1;
         else u = (o == this.collidables[m]) ? -2 : m - 1;
     }
     return (u == -2) ? m : -1;*/
	for(var i = 0; i < this.collidables.length; i++) {
		if(this.collidables[i] === o) {
			return i;
		}
	}
	return -1;
}

collisionEngine.prototype.update = function() {
	// use an insertion sort to update game objects based on their location and range
	for(var j = 1; j < this.collidables.length; j++) {
		var go = this.collidables[j];
		var i = j - 1;
	 
		while(i >= 0 && this.collidables[i].x > go.x) {
			this.collidables[i+1] = this.collidables[i];
			i = i - 1;
		}
	 
		this.collidables[i+1] = go;
	}
}

collisionEngine.prototype.checkForCollisions = function(go) {
	var collidedWith = [];
    
    /*var l = 0, u = this.collidables.length,  m;
    while ( l <= u ) { 
        var check = this.collidables[( m = Math.floor((l+u)/2) )];
        if ( go.x - go.range > check.x ) l = m+1;
        else u = (go == this.collidables[m]) ? -2 : m - 1;
    }
    if (u == -1) return collidedWith;*/
     
    for(var i = 0; i < this.collidables.length; i++) {
        var check = this.collidables[i];
        if(check.x > go.x + go.range + go.width) {
            break;
        }
        if(check.x + check.width >= go.x - go.range && check !== go) {
			if(go.inRange(check)) {
                collidedWith.push(check);
            }
        }
    }
	return collidedWith;
}
